package com.zlx.store.coupon.controller;

import com.zlx.store.common.results.HttpResp;
import com.zlx.store.coupon.service.CouponService;
import com.zlx.store.domain.entity.Coupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@RestController
@RequestMapping("/api/coupon")
public class CouponController {

    @Autowired
    CouponService couponService;

    /**
     * 添加一个新的优惠券
     *
     * @param coupon
     * @return
     */
    @PutMapping("/putCoupon")
    public HttpResp putCoupon(@RequestBody Coupon coupon) {
        return couponService.putCoupon(coupon);
    }

    /**
     * 删除优惠券,包括已经预热的
     *
     * @param couponId
     * @return
     */
    @DeleteMapping("/deleteCoupon")
    public HttpResp deleteCoupon(Long couponId) {
        return couponService.deleteCoupon(couponId);
    }

    /**
     * 修改优惠券
     *
     * @param coupon
     * @return
     */
    @PostMapping("/updateCoupon")
    public HttpResp updateCoupon(@RequestBody Coupon coupon) {
        return couponService.updateCoupon(coupon);
    }


    /**
     * 分页查看优惠券
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/showAllCoupon")
    public HttpResp showAllCoupon(int pageNum, int pageSize) {
        return couponService.showAllCoupon(pageNum, pageSize);
    }

    /**
     * 抢券接口
     *
     * @param couponId
     * @param userId
     * @return
     */
    @PutMapping("/snapUpCoupon")
    public HttpResp snapUpCoupon(Long couponId, Long userId) {
        //Long testUserId = (long) new Random().nextInt(500);


        return couponService.snapUpCoupon(couponId, userId);
    }


    /**
     * 添加优惠券到自动预热队列中
     *
     * @param couponId
     * @param userId
     * @return
     */
    @PutMapping("/addToPreQueue")
    public HttpResp addToPreQueue(Long couponId, Long userId) {
        return couponService.addToPreQueue(couponId);
    }

    /**
     * 手动预热优惠券
     *
     * @param couponId
     * @return
     */
    @PutMapping("/prepareCoupon")
    public HttpResp prepareCoupon(Long couponId) {
        return couponService.prepareCoupon(couponId);
    }

    /**
     * 从预热队列移除
     *
     * @param couponId
     * @param userId
     * @return
     */
    @DeleteMapping("/removeFromPreQueue")
    public HttpResp removeFromPreQueue(Long couponId, Long userId) {
        return couponService.removeFromPreQueue(couponId);
    }

    /**
     * 展示自动预热队列的内容
     *
     * @param userId
     * @return
     */
    @GetMapping("/showPreQueue")
    public HttpResp showPreQueue(Long userId) {
        return couponService.showPreQueue();
    }

    /**
     * 展示已经预热的优惠券
     *
     * @return
     */
    @GetMapping("/showCouponStock")
    public HttpResp showCouponStock() {
        return couponService.showCouponStock();
    }

    /**
     * 展示当前优惠券的数量
     *
     * @return
     */
    @GetMapping("/showCouponCount")
    public HttpResp showCouponCount() {
        return couponService.showCouponCount();
    }

    /**
     * 展示用户拥有的优惠券
     *
     * @param userId
     * @return
     */
    @GetMapping("/showCouponByUser")
    public HttpResp showCouponByUser(Long userId) {
        return couponService.showCouponByUser(userId);
    }

    /**
     * 当产生订单时调用此方法验证优惠券是否能使用
     *
     * @param couponId
     * @param userId
     * @param total
     * @return
     */
    @DeleteMapping("/useCoupon")
    public HttpResp useCoupon(Long couponId, Long userId, Double total) {
        return couponService.useCoupon(couponId, userId, total);
    }

    /**
     * 当订单被取消时调用此方法回滚被使用的优惠券
     *
     * @param couponId
     * @param userId
     * @return
     */
    @PutMapping("/rollBackCoupon")
    public HttpResp rollBackCoupon(Long couponId, Long userId) {
        return couponService.rollBackCoupon(couponId, userId);
    }

    /**
     * 注册用户后给新用户发放优惠券,只接受user服务器的调用
     * @param userId
     * @return
     */
    @PutMapping("/getCouponForNew")
    public HttpResp getCouponForNew(Long userId){
        return couponService.getCouponForNew(userId);
    }

}
